package com.example.hrmsaas.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hrmsaas.company.entity.Department;
import com.example.hrmsaas.system.entity.*;
import com.example.hrmsaas.system.mapper.PermissionMapper;
import com.example.hrmsaas.system.mapper.PermissionMenuMapper;
import com.example.hrmsaas.system.mapper.RoleMapper;
import com.example.hrmsaas.system.mapper.RolePermissionMapper;
import com.example.hrmsaas.system.service.IPermissionService;
import com.example.hrmsaas.system.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hrmsaas.system.service.IUserService;
import com.example.hrmsaas.system.utils.DataGridView;
import com.example.hrmsaas.system.utils.IdWorker;
import com.example.hrmsaas.system.utils.PermissionConstants;
import com.example.hrmsaas.system.utils.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 雷哥
 * @since 2021-11-09
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    private Log log = (Log) LogFactory.getLog(RoleServiceImpl.class);

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private IPermissionService permissionService;
    @Override
    public DataGridView getRoleList(RoleVo modelsVo) {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<Role>();
        if(null != modelsVo){
            if(null != modelsVo.getCompanyId()){
                roleQueryWrapper.eq("company_id",modelsVo.getCompanyId());
            }
        }
        roleQueryWrapper.orderByDesc("id");
        Page<Role> page = new Page<Role>(modelsVo.getPage(),modelsVo.getLimit());
        roleMapper.selectPage(page,roleQueryWrapper);
        return  new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public boolean addRold(Role role) {
        IdWorker idWorker = new IdWorker();
        String id = idWorker.nextId() + "";
        role.setId(id);
        if (roleMapper.insert(role) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean editRold(Role role) {
        Role oldRoelData = roleMapper.selectById(role.getId());
        oldRoelData.setName(role.getName() != null ? role.getName() : oldRoelData.getName());
        oldRoelData.setDescription(role.getDescription() != null ? role.getDescription() : oldRoelData.getDescription());
        if (roleMapper.updateById(oldRoelData) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean delRold(String id) {
        if (roleMapper.deleteById(id) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public DataGridView getRoleInfoById(String id)
    {
        Role data = roleMapper.selectById(id);
        //获取权限ids
        QueryWrapper<RolePermission> qrs = new QueryWrapper<RolePermission>();
        qrs.eq("role_id",id);
        List<RolePermission> list = rolePermissionMapper.selectList(qrs);
        log.info(list.toString());
        Set<String> permIds = new HashSet<String>();
        for(RolePermission rps:list){
            permIds.add(rps.getPermissionId());
        }
        data.setPremIds(permIds);
        return new DataGridView(data);
    }

    @Override
    public DataGridView getRoleListAll(RoleVo modelsVo) {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<Role>();
        if(null != modelsVo){
            if(null != modelsVo.getCompanyId()){
                roleQueryWrapper.eq("company_id",modelsVo.getCompanyId());
            }
        }
        roleQueryWrapper.orderByDesc("id");
        return  new DataGridView(roleMapper.selectList(roleQueryWrapper));
    }

    @Override
    public boolean setPrem(String role_id, List<String> prmeIds) {
        //1.获取角色实体
        Role role = roleMapper.selectById(role_id);
        //2.构建权限列表
        Set<Permission> prmes = new HashSet<>();
        for(String premId :prmeIds){
            Permission permission = permissionMapper.selectById(premId);
            List<Permission> apiList = permissionService.findByTypeAndPid(PermissionConstants.PY_API,permission.getId());
            prmes.addAll(apiList);
            prmes.add(permission);
        }
        log.info(prmes);
        if(null != prmes){
            //保存权限是看看是否存在已有的权限
            QueryWrapper<RolePermission> models = new QueryWrapper<RolePermission>();
            models.eq("role_id",role_id);
            List<RolePermission> Datas = rolePermissionMapper.selectList(models);
            //如果存在先删除原有的权限
            if(null != Datas){
                rolePermissionMapper.delete(models);
            }
            for(Permission prem:prmes){
                String PremId = prem.getId();
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(role_id);
                rolePermission.setPermissionId(PremId);
                rolePermissionMapper.insert(rolePermission);
            }
            return true;
        }else{
            return false;
        }
    }
}
